package sicau.carManager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import sicau.carManager.Util.TokenUtil.MD5Util;
import sicau.carManager.entity.Admin;
import sicau.carManager.mapper.AdminMapper;
import sicau.carManager.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ITcz
 * @since 2021-02-05
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    AdminMapper adminMapper;

    @Override
    public Admin loginIn(String account, String password) {
        String dbPassword = MD5Util.inputPassToDbPass(password);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("job_number", account);
        queryWrapper.eq("password", dbPassword);
        return adminMapper.selectOne(queryWrapper);
    }

    @Override
    public int addOne(Admin admin) {
        String dbPass = MD5Util.inputPassToDbPass(admin.getPassword());
        admin.setPassword(dbPass);
        return adminMapper.insert(admin);
    }

    @Override
    public int putImage(byte[] bytes, Integer id) {
        Admin admin = new Admin();
        admin.setImage(bytes);
        admin.setId(id);
        return adminMapper.updateById(admin);
    }

    @Override
    public byte[] pull(Integer id) {
        Admin admin = adminMapper.selectById(id);
        if (admin != null) {
            return admin.getImage();
        }else {
            return null;
        }
    }

    @Override
    public boolean updateOne(Admin admin) {
        return updateById(admin);
    }

    @Override
    public IPage<Admin> showAllAdmin(int pageNumber, int pageSize, String campus) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("authority" ,0);
        queryWrapper.eq("campus" ,campus);
        //查询工号和姓名
        queryWrapper.select("id", "job_number", "name");
        Page<Admin> p = new Page<>(pageNumber, pageSize);
        Page selectPage = adminMapper.selectPage(p, queryWrapper);
        return selectPage;
    }
}
